package com.snail.readyvr.rest;

import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.event.LoginEvent;
import com.snail.readyvr.mvp.model.LoginParam;
import com.snail.readyvr.mvp.model.LoginResponse;
import com.snail.readyvr.util.ACache;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.EventBus;
import com.snail.readyvr.util.Utils;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MRKING on 2016/11/9.
 */

public class LoginUtils {
    private LoginUtils() {
    }


    /**
     * @param loginRestClient 如果未使用注入的话，可以((MyApplication)MyApplication.getmContext())
     *                        .getApplicationComponent()
     *                        .getLoginRestClient()
     *                        方式获得 LoginRestClient
     * @param from
     * @param param
     */
    public static void getToken(LoginRestClient loginRestClient, final int from, final LoginParam param) {
        final LoginEvent loginEvent = new LoginEvent();
        loginEvent.setFrom(from);

        loginRestClient.login(param)
                .map(new HttpResultFunc<LoginResponse>(RestApiUtils.LOGIN_API))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        loginEvent.setType(LoginEvent.TYPE_FAILE);
                        loginEvent.setThrowable(e);
                        EventBus.getDefault().post(Constant.BusTag.LOGIN_TAG, loginEvent);
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        if (loginResponse != null) {
                            loginEvent.setType(LoginEvent.TYPE_SUCCESS);
                            Utils.token = loginResponse.getToken();
                            if (loginEvent.getFrom() != LoginEvent.FROM_VISTOR_LOGIN) {
                                Utils.sUser.setUserName(param.getUsername());
                                Utils.sUser.setEmailOrPhone(param.getUsername());
                                Utils.sUser.setHasbinding(true);
                                Utils.sUser.setToken(loginResponse.getToken());
                                Utils.sUser.setTokenTime(loginResponse.getExpire_time());
                                ACache.get(MyApplication.getmContext()).put(Constant.ACacheKey.KEY_USER, Utils.sUser, (int) (loginResponse.getExpire_time() - System.currentTimeMillis() / 1000));
                            }
                            EventBus.getDefault().post(Constant.BusTag.LOGIN_TAG, loginEvent);
                        } else {
                            loginEvent.setType(LoginEvent.TYPE_FAILE);
                            loginEvent.setThrowable(new RuntimeException(MyApplication.getmContext().getString(R.string.code_default)));
                            EventBus.getDefault().post(Constant.BusTag.LOGIN_TAG, loginEvent);
                        }
                    }
                });
    }
}
